package iscte.ai.kcgtd;

import java.util.ArrayList;
import java.util.Calendar;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.TextView;

public class CalendarActivity extends Activity {

	public Calendar month;
	public CalendarAdapter adapter;
	public Handler handler;
	public ArrayList<String> items; // container to store some random calendar items
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_calendar);
		
		month = Calendar.getInstance();

		KCGTDTime time = new KCGTDTime();
		month.set(time.getYear(), time.getMonth(), time.getMonthDay());
	    
	    items = new ArrayList<String>();
	    adapter = new CalendarAdapter(this, month);
	    
	    GridView gridview = (GridView) findViewById(R.id.calendar_gridview);
	    gridview.setAdapter(adapter);
	    
	    handler = new Handler();
	    handler.post(calendarUpdater);
	    
	    TextView title  = (TextView) findViewById(R.id.title);
	    title.setText(android.text.format.DateFormat.format("MMMM yyyy", month));
	    
	    TextView previous  = (TextView) findViewById(R.id.previous);
	    previous.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(month.get(Calendar.MONTH)== month.getActualMinimum(Calendar.MONTH)) {				
					month.set((month.get(Calendar.YEAR)-1),month.getActualMaximum(Calendar.MONTH),1);
				} else {
					month.set(Calendar.MONTH,month.get(Calendar.MONTH)-1);
				}
				refreshCalendar();
			}
		});
	    
	    TextView next  = (TextView) findViewById(R.id.next);
	    next.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(month.get(Calendar.MONTH)== month.getActualMaximum(Calendar.MONTH)) {				
					month.set((month.get(Calendar.YEAR)+1),month.getActualMinimum(Calendar.MONTH),1);
				} else {
					month.set(Calendar.MONTH,month.get(Calendar.MONTH)+1);
				}
				refreshCalendar();
				
			}
		});
	    
		gridview.setOnItemClickListener(new OnItemClickListener() {
		    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
		    	TextView date = (TextView)v.findViewById(R.id.calendar_item_date);
		        if(date instanceof TextView && !date.getText().equals("")) {
		        	String day = date.getText().toString();
		        	if(day.length()==1) {
		        		day = "0"+day;
		        	}
		        	
		        	Intent intent = new Intent(CalendarActivity.this, CalendarEventsActivity.class);
		        	intent.putExtra("iscte.ai.kcgtd.calendar_activity.event_date", android.text.format.DateFormat.format("yyyy-MM", month)+"-"+day);
		        	CalendarActivity.this.startActivity(intent);
		        }
		        
		    }
		});
		
	}
	
	/**
	 * refreshCalendar() method asks calendar adapter to refresh itself and changes month name.
	 */
	public void refreshCalendar(){
		TextView title  = (TextView) findViewById(R.id.title);
		
		adapter.refreshDays();
		adapter.notifyDataSetChanged();				
		handler.post(calendarUpdater); // generate some random calendar items				
		
		title.setText(android.text.format.DateFormat.format("MMMM yyyy", month));
	}
	
	/**
	 * Insert itens on the calendar
	 */
	public Runnable calendarUpdater = new Runnable() {
		@Override
		public void run() {
			items.clear();
			
			Calendar c = Calendar.getInstance();	
			ArrayList<OccupiedBlocks> aux = ProviderCalendar.getOccupiedBlocks(CalendarActivity.this, new KCGTDTime(0, MainActivity.settings.getStartMinutes(), MainActivity.settings.getStartHour(),
					month.getMinimum(Calendar.DAY_OF_MONTH), c.get(Calendar.MONTH), c.get(Calendar.YEAR)), new KCGTDTime(0, MainActivity.settings.getEndMinutes(), 
							MainActivity.settings.getEndHour(), month.getMaximum(Calendar.DAY_OF_MONTH), c.get(Calendar.MONTH), c.get(Calendar.YEAR)));
			
			for (OccupiedBlocks occupiedBlock : aux) {
				String day = String.valueOf(occupiedBlock.getStartTime().getMonthDay());
				
				if(!items.contains(day))
					items.add(day);
				
			}
			
			adapter.setItems(items);
			adapter.notifyDataSetChanged();
		}
	};

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.calendar, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

}
